package com.mrjoew.weekend.web.api;

import com.mrjoew.weekend.web.BaseController;
import com.mrjoew.weekend.entities.Response;
import com.mrjoew.weekend.entities.dto.RegisterDTO;
import com.mrjoew.weekend.services.bz.RegisterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @author Christopher Jones
 * @ClassName RegisterApiController
 * @explain
 * @date 2020/11/7 0007 21:11
 */
@Api(tags = "注册")
@RequestMapping(value = "/api/register")
@RestController
public class RegisterApiController extends BaseController {

    private final RegisterService registerService;

    public RegisterApiController(RegisterService registerService) {
        this.registerService = registerService;
    }

    @ApiOperation("账号注册")
    @PutMapping()
    public Response<Object> register(@Validated @RequestBody RegisterDTO in, BindingResult bindingResult) {
        if (bindingResult.getErrorCount() > 0) {
            return Response.error(Response.ERROR_VALIDATE, Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }

        registerService.register(in);
        return Response.ok();
    }
}
